<?php
// 本类由系统自动生成，仅供测试用途
class WeeklistAction extends CommonAction {
    public function index(){
        if(isset($_SESSION['loginUserName'])){
            $this->assign('userName',$_SESSION['loginUserName']);
        }

        $curtime    = $_GET['week']?$_GET['week']:date(time());//不是表示当前时间，是当前要显示的时间
        $curYear    = date("Y",$curtime);
        $curWeek    = date("W",$curtime);

        $this->assign('preweek',$curtime-7*24*3600);
        $this->assign('nextweek',$curtime+7*24*3600);
        $this->assign('curweek',date(time()));

        //$week         = getWeekStartAndEnd($curYear,$curWeek);
        $weekStart  = this_monday($curtime,1);//$week['start'];
        $weekEnd    = this_sunday($curtime,1);//$week['end'];
        $_SESSION['weekStart']  = $weekStart;//主要用于在导出表格excel时使用
        $_SESSION['weekEnd']    = $weekEnd;//主要用于在导出表格excel时使用
        $wsList     = array("星期一","星期二","星期三","星期四","星期五","星期六","星期日");

        $list       = array();

        $weeklist   = M('Weeklist');
        if(check_username(C('WEEKLIST_EDIT_USERLIST'))){
            $list       = $weeklist->where("riqi>=$weekStart and riqi <=$weekEnd")->order("riqi,sort desc")->select();
        }else{
            $list       = $weeklist->where("riqi>=$weekStart and riqi <=$weekEnd and publish = 1")->order("riqi,sort desc")->select();
        }
        //dump($list);
        if($list && isset($_SESSION['loginUserName'])){
            $this->assign('waFlag',1);//用于在模板显示是否使用空白表还是使用数据库数据
            $wsList1     = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
            //补齐一周的全部空格，包括上下午，哪一天没有就补上空格，并按日期顺序排序，为了方便排序，下午全部加了1ms
            for($i=0;$i<7;$i++){
                $t1 = $weekStart + $i*24*60*60;
                $w1 = $wsList1[date('w',$t1)];
                $c  = count($list);
                $flagd = 0;
                for($j=0;$j<$c;$j++){
                    //echo $t1.'  '.$list[$j]['riqi'].'<br>';
                    //判断这一天有没有list数据输出
                    if($t1==$list[$j]['riqi']){
                        $flagd = 1;
                    }
                }
                //$flagd==0则表示这一天在数据list里没有记录，要加上空白记录
                if($flagd==0){
                    $temp =array(array("riqi"=>$t1,"week"=>$w1,"emptyf"=>"emptyf"));
                    array_splice($list,0,0,$temp);
                }
            }//for
        }else{//显示空表
            $this->assign('waFlag',0);
            for($i=0;$i<7;$i++){
                $showDate   = $weekStart + $i*24*60*60;
                $list[$i]   = array("showWeek"=>$wsList[$i],"showDate"=>$showDate);
            }
        }
        //dump($list);
        //return;
        //$this->array_sort_by($list,'sxw');
        $this->array_sort_by($list,'riqi');

        $this->assign('list',$list);      

        //以下代码用于显示学校列表
        //$School     = M('School');
        //$list1      = $School->where('status = 1')->order('sortid')->field('id,sName,sType')->select();//getField('id,sName,sType',":");
        //$this->assign('schoolJson',json_encode($list1)); 

        $this->display();
        return;
    }
    /**
    * 对一个多维数组的某个字段进行排序
    *
    * @param array $array 待排序数组
    * @param string $key 排序根据的字段
    * @return array 子数组的字段
    */
    function array_sort_by(&$array,$key,$sub){
        $tmp = null;
        $min = null;
        $n = count($array);
        for ($i=0;$i<=$n-1;$i++){
          $min = $i;
          if(isset($array[$i+1])){
           for($y=$i+1;$y<$n;$y++){
            if($array[$min][$key]>
            $array[$y][$key]){
             $min = $y;
            }
           }
           if($min!=$i){
            $tmp = $array[$i];
            $array[$i] = $array[$min];
            $array[$min] = $tmp;
            unset($tmp);
           }
          }
          if(isset($array[$i][$sub])) {
           $array[$i][$sub]=array_sort_by($array[$i][$sub],$key);
          }
        }
        unset($n);
        unset($min);
        return $array;
    }

    //审核审批
    public function setCheckFlag(){
        $curtime    = $_GET['week']?$_GET['week']:date(time());//不是表示当前时间，是当前要显示的时间
        $weekStart  = this_monday($curtime,1);//$week['start'];
        $weekEnd    = this_sunday($curtime,1);//$week['end'];

        $weeklist   = M('Weeklist');
        if(check_username(C('WEEKLIST_CHECK_FLAG1'))){
            $r1 = $weeklist->where("riqi>=$weekStart and riqi <=$weekEnd")->setField('checkflag1',1);
            if($r1){
                echo"审核成功！";
                return true;
            }else{
                echo"请勿重复审核!";return false;
            }

        }
        if(check_username(C('WEEKLIST_CHECK_FLAG2'))){
            $r2 = $weeklist->where("riqi>=$weekStart and riqi <=$weekEnd")->setField('checkflag2',1);
            if($r2){
                echo"审批成功！";
                return true;
            }else{
                echo"请勿重复审批!";return false;
            }
        }
        echo"没有权限或者错误操作！";
        return false;
    }
    //发布、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
    public function setpublishFlag(){
        $curtime    = $_GET['week']?$_GET['week']:date(time());//不是表示当前时间，是当前要显示的时间
        $weekStart  = this_monday($curtime,1);//$week['start'];
        $weekEnd    = this_sunday($curtime,1);//$week['end'];

        $weeklist   = M('Weeklist');
        if(check_username(C('WEEKLIST_PUBLISH_FLAG'))){
            $r1 = $weeklist->where("riqi>=$weekStart and riqi <=$weekEnd")->setField('publish',1);
            if($r1){
                echo "成功发布".$r1."条信息！";
                return true;
            }else{
                echo"信息只有经审核审批之后才可以发布!<BR>没有找到未发布内容！";return false;
            }
        }
        echo"没有权限或者错误操作！";
        return false;
    }

    //删除自己userid的记录
    public function delSelf(){
        $name = $this->getActionName();
        $model = D($name);
        if (!empty($model)) {
            $pk = $model->getPk();
            $id = $_REQUEST [$pk];
            if (isset($id)) {
                $condition = array($pk => array('in', explode(',', $id)));
                if(strpos(C('UNLIMITE_USER'), $_SESSION['loginUserName'])){
                    //非受限用户可以任意删除
                }else{
                    $condition['userid'] = $_SESSION[C('USER_AUTH_KEY')];
                }
                if ($model->where($condition)->delete() > 0) {
                    $this->success('删除成功！',true);
                } else {
                    $this->error('删除失败！',false);
                }
            } else {
                $this->error('非法操作',false);
            }
        }
        $this->forward();
    }


    //向上排序，排序号加1
    public function sortup(){
        $id         = $this->_get('id');
        if(strpos(C('WEEKLIST_CHECK_FLAG3'), $_SESSION['loginUserName'])){
            $weeklist = M("weeklist"); // weeklist
            $weeklist->where('id='.$id)->setInc('sort',1); // 
            $this->redirect('Weeklist/index');
        }
    }
}
?>